<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\blog\model;
use think\Model;
use think\db;

class GoodsbackupModel extends Model
{
    public  function session()
    {
        $id=cmf_get_current_admin_id();
        $where['agency_id']=$id;
        $session=DB::name('shop')->where($where)->find();
        return $session;
    }
   /*类别列表*/
   public function index($cause)
   {
       $session=$this->session();
       $where['bc.shop_id']=$session['id'];
       if($cause)
       {
           $where['bc.state_name']=array('like',"%".$cause."%");
       }
       $data=DB::name('goods_backup')->alias('bc')->join('shop sp','sp.id = bc.shop_id')->where($where)->field('bc.*,sp.shop_name')->paginate(10);
       if($data->toArray())
       {
         foreach ($data as $k=>$v)
         {
             $data->toArray();
             $date=array();
             $date=$v;
             if($v['status']==1){
                 $date['status_name']='启用';
             }else{
                 $date['status_name']='禁用';
             }
             $data->offsetSet($k,$date);
         }

       }
       return $data;

   }
   /*
    * 添加类别
    * */
   public function add($data)
   {
       $session=$this->session();
       $array['gd_guid']=guid();
       $array['state_name']=$data['state_name'];
       $array['status']=1;
       $array['shop_id']=$session['id'];
       $array['c_time']=time();
       $status=Db::name('goods_backup')->insert($array);
       return $status;
   }
   /*修改类别*/
   public function detail($cause,$data)
   {
       $session=$this->session();
       $where=$cause;
       $where['shop_id']=$session['id'];
     $status=DB::name('goods_backup')->where($where)->update($data);
     return $status;
   }
   /*
    * 删除
    * 先判断是否有该类别的商品 然后看 是否有该类别的订单
    * */
   public function del($cause)
   {   $session=$this->session();
       $where['shop_id']=$session['id'];
       $where['nature']=$cause['id'];
       $order=DB::name('order_detail')->where($where)->find();
       if($order)
       {
           $status['num']=0;
           $status['con']='类别下有订单，不能删除！';
       }else{
           $cause['shop_id']=$session['id'];
           $goodsclass=DB::name('goods_backup')->delete($cause);
           if($goodsclass)
           {
               $status['num']=1;
               $status['con']='删除成功';
           }else{
               $status['num']=0;
               $status['con']='删除失败！';
           }
       }
       return $status;
   }
   /**
    * 根据id获取列表记录
   */
   public function getGcone($where){
       $session=$this->session();
       $where['shop_id']=$session['id'];
       $status= DB::name('goods_backup')->where($where)->find();
       return $status;
   }

}